import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';

import { UserAuthService } from '@core/services/user-auth.service';
import { InitService } from '@core/services/init.service';
import { ApesMessageService } from '@shared/apes/message';
import { WebsocketService } from '@core/services/websocket/websocket.service';
import { Platform } from '@angular/cdk/platform';
import {ApiService} from "@core/services/http/api.service";
import {AnalysisService} from "../../routes/analysis/analysis.service";

@Injectable()
export class CanAuthProvide implements CanActivate {

  constructor(private userAuthService: UserAuthService,
              private router: Router,
              private platform: Platform,
              private msg: ApesMessageService,
              private initService: InitService,
              private ws: WebsocketService,
              private as:AnalysisService) {
  }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {

    // Pad 端逻辑处理
    // if( this.platform.ANDROID ){
    //     // 防止在 Pad端登录的时候，进入 '' 路由，直接转入 login 界面
    //     this.router.navigateByUrl('/passport');
    //     return false;
    // }

    if (this.userAuthService.isLogin()) {
      this.initService.loadAppData();
      this.as.getLocalPermit(this.userAuthService.getUserInfoObj().userAccount.post.localPermit.id);
      this.ws.connect(this.userAuthService.getUserInfoObj().userAccount.id);
      return true;
    } else {
      this.msg.error('请先登录');
      this.router.navigateByUrl('/passport');
      return false;
    }
  }

}
